fix(parser): preserve semicolons in inline comments#982
Conversation
- Keep all text after the first unescaped semicolon when parsing inline comments, instead of only taking the first split segment. - Add tests for normal inline comments and comment-only lines with multiple semicolons.
There was a problem hiding this comment.
Code Review
This pull request updates the script parser to correctly preserve semicolons that appear within inline comments or comment-only lines, and adds unit tests to verify this behavior. The review feedback points out a performance optimization opportunity: instead of splitting the entire string by all unescaped semicolons and joining them back, the parser can find the first unescaped semicolon using a regular expression and split the string using substring, avoiding unnecessary array allocations.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| const commentSplit = sentenceRaw.split(/(?<!\\);/); | ||
| let newSentenceRaw = commentSplit[0]; | ||
| newSentenceRaw = newSentenceRaw.replaceAll('\\;',';'); | ||
| const sentenceComment = commentSplit[1] ?? ''; | ||
| const sentenceComment = commentSplit.slice(1).join(';'); |
There was a problem hiding this comment.
使用 split 正则表达式然后 slice(1).join(';') 的做法效率较低,因为它会把整行文本按照所有未转义的分号全部切分,生成一个字符串数组,然后再把它们重新拼接起来。\n\n实际上,我们只需要找到第一个未转义的分号,然后将字符串一分为二即可。使用 RegExp.prototype.exec 找到第一个匹配项的索引,再通过 substring 截取,可以完全避免不必要的数组分配和字符串拼接,从而提升解析器的性能(特别是在注释中包含较多分号的情况下)。
const match = /(?<!\\);/.exec(sentenceRaw);\n let newSentenceRaw = match ? sentenceRaw.substring(0, match.index) : sentenceRaw;\n newSentenceRaw = newSentenceRaw.replaceAll('\\;', ';');\n const sentenceComment = match ? sentenceRaw.substring(match.index + 1) : '';
修正存在多个
;时,行内注释未正确截取的问题